// hyb_yl/doctorHome/pages/distribution/distribution.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    j_id: "",
    f_id: "",
    label_ids: "",
    jz_time: "请选择就诊时间",
    f_ids: "",
    zid:"",
  },
  add_label:function(e){
    this.setData({
      hiddenmodalput:true
    })
  },
  cancel:function(){
    this.setData({
      hiddenmodalput:false
    })
  },
  labname_input:function(e){
    this.setData({
      addlabname:e.detail.value
    })
  },
  confirm:function(e){
    var that = this;
    
    var addlabname=that.data.addlabname;
    app.util.request({
      'url':"entry/wxapp/follow.add_label",
      data:{
        title:addlabname,
        zid:that.data.zid,
      },
      success:function(res)
      {
        wx.showToast({
          title: res.data.message,
          icon:"none"
        })
        that.setData({
          hiddenmodalput:false
        })
        that.getLabel()
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var j_id = options.j_id;
    var that = this;
    that.setData({
      j_id: j_id,
      id: options.id,
      zid:options.zid
    })
    that.getLabel();
    that.getFollow_list();
  },
  // 获取标签
  getLabel: function () {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/follow.Label",
      data:{
        zid:that.data.zid
      },
      success: function (res) {
        that.setData({
          label_arr: res.data
        })
      }
    })
  },
  checkboxChange: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    var indexs = e.currentTarget.dataset.indexs;
    var list = this.data.follow_list;
    
    list[indexs].list[index].checked = !list[indexs].list[index].checked;
    that.setData({
      follow_list: list,
    })
    console.log(that.data.follow_list)
  },
  // 获取随访计划
  getFollow_list: function () {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/follow.Follow_list",
      success: function (res) {
        that.setData({
          follow_list: res.data
        })
      }
    })
  },
  time_choose: function (e) {
    this.setData({
      jz_time: e.detail.value
    })
  },
  // 选择标签
  change_label: function (e) {
    var index = e.currentTarget.dataset.index;
    var label_arr = this.data.label_arr;

    label_arr[index].checked = true;
    this.setData({
      label_arr: label_arr
    })
  },
  tijiao: function () {
    var that = this;
    var label_arr = that.data.label_arr;
    var jz_time = that.data.jz_time;
    var j_id = that.data.j_id;
    var id = that.data.id;
    var label_ids = "";
    for (var i = 0; i < label_arr.length; i++) {
      if (label_arr[i].checked) {
        label_ids += label_arr[i].id + ','
      }
    }
    var follow_list = that.data.follow_list;
    var f_ids = "";
    for (var i = 0; i < follow_list.length; i++) {
      for (var j = 0; j < follow_list[i].list.length; j++) {
        if(follow_list[i].list[j].checked)
        {
          f_ids += follow_list[i].list[j].id + ','
        }
        
      }
    }
    console.log(f_ids);
    f_ids = f_ids.substring(0, f_ids.length - 1);
    console.log(f_ids);
    label_ids = label_ids.substring(0, label_ids.length - 1);
    if (label_ids == '') {
      wx.showToast({
        title: '请选择患者标签',
      })
    } else if (jz_time == '请选择就诊时间') {
      wx.showToast({
        title: '请选择就诊时间',
      })
    } else if (f_ids == '') {
      wx.showToast({
        title: '请选择随访计划',
      })
    } else {
      app.util.request({
        'url': "entry/wxapp/follow.add_suifang",
        data: {
          // openid: wx.getStorageSync('openid'),
          j_id: that.data.j_id,
          label_ids: label_ids,
          jz_time: jz_time,
          f_ids: f_ids,
          zid: wx.getStorageSync('zid'),
        },
        success: function (res) {
          wx.navigateTo({
            url: '/hyb_yl/doctorHome/pages/result_task/result_task?zid='+wx.getStorageSync('zid'),
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})